package com.qq.BFMRSE.trapDoor;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.qq.BFMRSE.entity.encryptedIndex;
import com.qq.BFMRSE.index.indexBuildTool;
import com.qq.BFMRSE.index.indexEncrypted;

import Jama.Matrix;


public class EncryptedTroopHelper {
	public static double[]  SpiltDouble(double x)
	{
		double[] result=new double[2];
		Random random=new Random();
		if(x!=0)
		{
			result[0]=random.nextInt((int)x);
			result[1]=x-result[0];
		}
		else {
			result[0]=result[1]=0;
		}
		
		return result;
	}
	public static encryptedIndex encrypTropDoor(indexEncrypted SK,double []trapdoorPlain)
	{
		encryptedIndex res=new encryptedIndex();
		int []S=SK.getS();
		Matrix M1=SK.getM1();
		Matrix M2=SK.getM2();
		double []tempMD1=new double[trapdoorPlain.length];
		double []tempMD2=new double[trapdoorPlain.length];
		for(int i=0;i<S.length;i++)
		{
			if(S[i]==1)
			{
				tempMD1[i]=tempMD2[i]=trapdoorPlain[i];
			}
			else {
				double []sp=SpiltDouble(trapdoorPlain[i]);
				tempMD1[i]=sp[0];
				tempMD2[i]=sp[1];
			}
		}
		Matrix encryMatrix1=M1.inverse().times(new Matrix(tempMD1,1).transpose());
		Matrix encryMatrix2=M2.inverse().times(new Matrix(tempMD2,1).transpose());
		tempMD1=indexBuildTool.getArrayFromMatrix(encryMatrix1);
		tempMD2=indexBuildTool.getArrayFromMatrix(encryMatrix2);
		res.setMD1(tempMD1);
		res.setMD2(tempMD2);
		return res;
		
	}
}
